package hms.RoomManagementModule;

import hms.rooms.Room;
import hms.rooms.RoomType;
import hms.rooms.RoomTypeTable;

import java.awt.BorderLayout;
import java.awt.Dialog;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListSelectionModel;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;

public class MorePanel extends JDialog {

	private static final long serialVersionUID = 1L;
	
	private Room room;
	private final JPanel contentPanel = new JPanel();
	private JButton okButton;
	
	protected JButton btn_additionalNotes;
	
	protected JLabel lbl_roomLabel, lbl_status, lbl_roomType, lbl_price, lbl_smoking,
		lbl_bedSize, lbl_numberOfBeds, lbl_amenities;
	
	protected JList list_amenities;
	
	protected DefaultListModel listModel;

	/**
	 * Set the values for the labels and list to correspond to the values for the selected
	 * room and it's room type
	 */
	public void setValuesForComponents(){
		
		// Room Information
		lbl_roomLabel.setText("Room: " + room.getRoomLabel());
		lbl_status.setText("Status: " + room.getOccupationStatus());
		lbl_roomType.setText("Type: " + room.getRoomTypeLabel());
		
		// Room Type Information
		RoomType roomType = (new RoomTypeTable()).getRoomTypeByLabel(room.getRoomTypeLabel());
		lbl_price.setText("Price per Night: $" + roomType.getPricePerNight());
		if(roomType.getSmokingAllowed() == 'T')
			lbl_smoking.setText("Smoking: Yes");
		else
			lbl_smoking.setText("Smoking: No");
		lbl_bedSize.setText("Bed Size: " + roomType.getBedSize());
		lbl_numberOfBeds.setText("Number of Beds: " + roomType.getNumberOfBeds());
	
		// Fill list with amenities
		LinkedList amenitiesList = roomType.getAmenitiesList();
		if(!amenitiesList.isEmpty()){
			for(int i = 0; i < amenitiesList.size(); i++){
				listModel.addElement(amenitiesList.get(i).toString());
			}
		}
	
	
	}
	
	/**
	 * Create the dialog.
	 */
	public MorePanel(Room i_room, ManageRoomsView view) {
		room = i_room;
		setAlwaysOnTop(true);
		setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle("More Information");
		setBounds(100, 100, 611, 412);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		
		listModel = new DefaultListModel();
		
		lbl_roomLabel = new JLabel("Room: ");
		lbl_roomLabel.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_status = new JLabel("Status:");
		lbl_status.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_roomType = new JLabel("Type:");
		lbl_roomType.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_price = new JLabel("Pricer per Night: ");
		lbl_price.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_smoking = new JLabel("Smoking:");
		lbl_smoking.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_bedSize = new JLabel("Bed Size: ");
		lbl_bedSize.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_numberOfBeds = new JLabel("# of Beds: ");
		lbl_numberOfBeds.setFont(new Font("SansSerif", Font.PLAIN, 16));
		lbl_amenities = new JLabel("Included Amenities:");
		lbl_amenities.setFont(new Font("SansSerif", Font.PLAIN, 16));
		
		list_amenities = new JList(listModel);
		list_amenities.setBorder(new BevelBorder(BevelBorder.LOWERED, null, null, null, null));
		list_amenities.setVisibleRowCount(15);
		list_amenities.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel.setHorizontalGroup(
			gl_contentPanel.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(337)
					.addComponent(lbl_amenities, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
					.addGap(59))
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createParallelGroup(Alignment.TRAILING, false)
							.addComponent(lbl_roomLabel, Alignment.LEADING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(lbl_status, Alignment.LEADING, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
						.addComponent(lbl_roomType, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_price, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_smoking, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_bedSize, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE)
						.addComponent(lbl_numberOfBeds, GroupLayout.PREFERRED_SIZE, 275, GroupLayout.PREFERRED_SIZE))
					.addGap(52)
					.addComponent(list_amenities, GroupLayout.DEFAULT_SIZE, 248, Short.MAX_VALUE)
					.addContainerGap())
		);
		gl_contentPanel.setVerticalGroup(
			gl_contentPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPanel.createSequentialGroup()
					.addGap(14)
					.addComponent(lbl_amenities, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addGroup(gl_contentPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_contentPanel.createSequentialGroup()
							.addComponent(lbl_roomLabel)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_status, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_roomType, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_price, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_smoking, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_bedSize, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lbl_numberOfBeds, GroupLayout.PREFERRED_SIZE, 21, GroupLayout.PREFERRED_SIZE))
						.addComponent(list_amenities, GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE))
					.addContainerGap())
		);
		contentPanel.setLayout(gl_contentPanel);
		{
			JPanel buttonPane = new JPanel();
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						clickOkButton();
					}
				});
				okButton.setActionCommand("OK");
				getRootPane().setDefaultButton(okButton);
			}
			
			btn_additionalNotes = new JButton("Additional Notes");
			btn_additionalNotes.setActionCommand("btn_additionalNotes");
			btn_additionalNotes.addActionListener(view);
			GroupLayout gl_buttonPane = new GroupLayout(buttonPane);
			gl_buttonPane.setHorizontalGroup(
				gl_buttonPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_buttonPane.createSequentialGroup()
						.addContainerGap()
						.addComponent(okButton, GroupLayout.PREFERRED_SIZE, 84, GroupLayout.PREFERRED_SIZE)
						.addPreferredGap(ComponentPlacement.RELATED, 367, Short.MAX_VALUE)
						.addComponent(btn_additionalNotes, GroupLayout.PREFERRED_SIZE, 134, GroupLayout.PREFERRED_SIZE)
						.addContainerGap())
			);
			gl_buttonPane.setVerticalGroup(
				gl_buttonPane.createParallelGroup(Alignment.LEADING)
					.addGroup(gl_buttonPane.createSequentialGroup()
						.addContainerGap()
						.addGroup(gl_buttonPane.createParallelGroup(Alignment.BASELINE)
							.addComponent(btn_additionalNotes, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
							.addComponent(okButton, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))
						.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
			);
			buttonPane.setLayout(gl_buttonPane);
		}
		
		setValuesForComponents();
	}
	
	public void clickOkButton(){
		this.dispose();
	}
}
